﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiaoYuStock.Model.Models;
using XiaoYuStock.Model.Request;
using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Core;
using System.Threading.Tasks;
using XiaoYuStock.Core.Entities;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Entities;
using XiaoYuStock.AppService;

namespace XiaoYuStock.WebApp.Controllers
{
    public class HistoryDataController : BaseController
    {

        private HistoryDataBusiness historyBus = new HistoryDataBusiness();

        private DoctorBusiness doctorBus = new DoctorBusiness();
        //
        // GET: /Book/

        public ActionResult Index(HistoryRequest request)
        {
            request.PageSize = 20;

            var myShares= new ShareTargetBusiness().GetList(new ShareTargetRequest() {  PageIndex=1, PageSize=50});
            if (!myShares.Any())
                myShares = new List<SharesInfo>();
            var t=myShares.ToList();
            t.Insert(0, new SharesInfo() { ShareCode = "0", ShareName = "不限" });
            ViewData.Add("CustomerCode", new SelectList(t, "ShareCode", "ShareName", request.CustomerCode));
            if (request.CustomerCode == "0")
                request.CustomerCode = null;
            var result = historyBus.GetList(request);
           
            return View(result);
        }
      
       

        [HttpPost]
        public ActionResult GetHistoryUpdate(string sharesCodes)
        {
            if (!string.IsNullOrWhiteSpace(sharesCodes))
            {
                string [] a= sharesCodes.Split('|');
                if (a != null && a.Length > 0)
                {

                    foreach (var b in a)
                    {
                        var season = (DateTime.Now.Month / 3-1) + 1;
                        DataMapping.SynUpdateHistoryData(b, DateTime.Now.Year, season);
                    }
                   
                }
            }
            else
                return Json(new {  status=false});
            return Json(new { status = true });
        }

        public ActionResult ShowDoctor(string shareCode,DateTime date)
        {
            var model = new XiaoYuStock.Core.Entities.DoctorData();
            var data = doctorBus.Get(shareCode);
            if (data != null)
            {
                model = JsonHelper.FromJson<XiaoYuStock.Core.Entities.DoctorData>(data.TContent);
            }
            return View(model);
        }
    
    }


}
